var BaseUedit = app.controller('BaseUedit', function ($rootScope, $scope, $http, $state, $cordovaCamera, $cordovaFileTransfer, $cordovaProgress, BaseUeditData,BaseUeditDefault) {
	try {
		$rootScope.showf = false;
		$rootScope.title = '个人信息';

		$scope.show = false;
		$scope.mIsTutor=false;
		$scope.mIsTutor=$rootScope.istutor;

		$scope.tid="head";

		//获取用户信息
		$scope.user = {};
		var url = SITE_URL + "index.php?app=baseloginapp&callback=JSON_CALLBACK&act=userinfo";
		$http.jsonp(url).success(function (data) {
			$scope.user = data.retval;
			if ($rootScope.btrs1_real_name != undefined && $rootScope.btrs1_real_name != "" && $rootScope.btrs1_real_name != null) {
				if ($scope.user.real_name != $rootScope.btrs1_real_name) {
					var url = SITE_URL + "index.php?app=baseloginapp&callback=JSON_CALLBACK&act=useredit" +
					"&real_name=" + $rootScope.btrs1_real_name;
					$http.jsonp(url).success(function (data) {

					});
				}
				$scope.user.real_name = $rootScope.btrs1_real_name;
			}
			if ($rootScope.btrs1_organization != undefined && $rootScope.btrs1_organization != "" && $rootScope.btrs1_organization != null) {
				if ($scope.user.organization != $rootScope.btrs1_organization) {
					var url = SITE_URL + "index.php?app=baseloginapp&callback=JSON_CALLBACK&act=useredit" +
					"&real_name=" + $scope.user.real_name
					+ "&organization=" + $rootScope.btrs1_organization;
					$http.jsonp(url).success(function (data) {
					});
				}
				$scope.user.organization = $rootScope.btrs1_organization;
			}

			if ($rootScope.btrs1_position != undefined && $rootScope.btrs1_position != "" && $rootScope.btrs1_position != null) {
				if ($scope.user.position != $rootScope.btrs1_position) {
					var url = SITE_URL + "index.php?app=baseloginapp&callback=JSON_CALLBACK&act=useredit" +
					"&real_name=" + $scope.user.real_name
					+ "&position=" + $rootScope.btrs1_position;
					$http.jsonp(url).success(function (data) {
					});
				}
				$scope.user.position = $rootScope.btrs1_position;
			}
			if ($rootScope.btrs1_user_sex != undefined && $rootScope.btrs1_user_sex != "" && $rootScope.btrs1_user_sex != null) {
				if ($scope.user.gender != $rootScope.btrs1_user_sex) {
					var sex = $rootScope.btrs1_user_sex == '保密' ? 0 : $rootScope.btrs1_user_sex == '男' ? 1 : 2;
					var url = SITE_URL + "index.php?app=baseloginapp&callback=JSON_CALLBACK&act=useredit" +
					"&real_name=" + $scope.user.real_name
					+ "&sex=" + sex;
					$http.jsonp(url).success(function (data) {
					});
				}
				$scope.user.gender = $rootScope.btrs1_user_sex;
			}
			if ($rootScope.btrs1_user_age != undefined && $rootScope.btrs1_user_age != "" && $rootScope.btrs1_user_age != null) {
				if ($scope.user.age != $rootScope.btrs1_user_age) {
					var url = SITE_URL + "index.php?app=baseloginapp&callback=JSON_CALLBACK&act=useredit" +
					"&real_name=" + $scope.user.real_name
					+ "&age=" + $rootScope.btrs1_user_age;
					$http.jsonp(url).success(function (data) {
					});
				}
				$scope.user.age = $rootScope.btrs1_user_age;
			}
			if ($rootScope.btrs1_city != undefined && $rootScope.btrs1_city != "" && $rootScope.btrs1_city != null) {
				if ($scope.user.city != $rootScope.btrs1_city) {
					var url = SITE_URL + "index.php?app=baseloginapp&callback=JSON_CALLBACK&act=useredit" +
					"&real_name=" + $scope.user.real_name
					+ "&city=" + $rootScope.btrs1_city;
					$http.jsonp(url).success(function (data) {
					});
				}
				$scope.user.city = $rootScope.btrs1_city;
			}

			if ($rootScope.btrs1_trade != undefined && $rootScope.btrs1_trade != "" && $rootScope.btrs1_trade != null) {
				if ($scope.user.trade != $rootScope.btrs1_trade) {
					var url = SITE_URL + "index.php?app=baseloginapp&callback=JSON_CALLBACK&act=useredit" +
					"&real_name=" + $scope.user.real_name
					+ "&trade=" + $rootScope.btrs1_trade;
					$http.jsonp(url).success(function (data) {
					});
				}
				$scope.user.trade = $rootScope.btrs1_trade;
			}

			$scope.iniconfig();
		});

		//修改昵称
		$scope.go_real_name = function () {
			if ($scope.user.real_name != undefined && $scope.user.real_name != null) {
				$rootScope.btrs1_real_name = $scope.user.real_name;
			}
			$rootScope.go('nickname');
		}
		//修改组织
		$scope.go_organization = function () {
			if ($scope.user.organization != undefined && $scope.user.organization != null) {
				$rootScope.btrs1_organization = $scope.user.organization;
			}
			if ($scope.user.position != undefined && $scope.user.position != null) {
				$rootScope.btrs1_position = $scope.user.position;
			}
			$rootScope.go('tutorop');
		}
		//修改城市
		$scope.go_city = function () {
			if ($scope.user.city != undefined && $scope.user.city != null) {
				$rootScope.btrs1_city = $scope.user.city;
			}
			$rootScope.go('tutorcity');
		}
		//修改行业
		$scope.go_trade = function () {
			if ($scope.user.trade != undefined && $scope.user.trade != null) {
				$rootScope.btrs1_trade = $scope.user.trade;
			}
			$rootScope.go('tutortrade');
		}
		//修改性别
		$scope.go_sex = function () {
			if ($scope.user.gender != undefined && $scope.user.gender != null) {
				$rootScope.btrs1_user_sex = $scope.user.gender;
			}
			$rootScope.go('usersex');
		}
		//修改年龄层
		$scope.go_age = function () {
			if ($scope.user.ager != undefined && $scope.user.age != null) {
				$rootScope.btrs1_user_age = $scope.user.gender;
			}
			$rootScope.go('userage');
		}
		$scope.iniconfig = function () {
			$rootScope.btrs1_tutor_name = "";
			$rootScope.btrs1_real_name = "";
			$rootScope.btrs1_tel = "";
			$rootScope.btrs1_city = "";
			$rootScope.btrs1_trade = "";
			$rootScope.btrs1_organization = "";
			$rootScope.btrs1_position = "";
			$rootScope.btrs1_email = "";
			$rootScope.btrs1_user_age = "";
			$rootScope.btrs1_user_sex = "";
		}
		//关闭本页 清空全局变量
		$scope.finishself = function () {
			$scope.iniconfig();
			$rootScope.back();
		}
		//保存修改
		$scope.save = function () {
			$scope.iniconfig();
			var real_name = $scope.user.real_name;
			var city = $scope.user.city;
			var trade = $scope.user.trade;
			var organization = $scope.user.organization;
			var position = $scope.user.position;
			var sex = $scope.user.gender == '保密' ? 0 : $scope.user.gender == '男' ? 1 : 2;
			var age = $scope.user.age;

			if (real_name == "" || real_name == undefined || real_name == null) {
				showtoast("请填写昵称");
				return false;
			}
			var url = SITE_URL + "index.php?app=baseloginapp&callback=JSON_CALLBACK&act=useredit" +
			"&real_name=" + real_name +
			"&city=" + city +
			"&trade=" + trade +
			"&organization=" + organization +
			"&position=" + position +
			"&sex=" + sex +
			"&age=" + age;
			$http.jsonp(url).success(function (data) {
				if (data.done) {
					showtoast(data.msg);
					$rootScope.back();
				}
			});
		}
		$scope.imgclick=function(t){
			$scope.tid=t;
			$scope.show=true;
		}


		//拍照
		$scope.takephoto = function (e) {

			if (e) {
				e.stopPropagation();
			}
			var act="upheaderimg";
			var cutWidth=300;
			var cutHeight=300;
			if($scope.tid=="head"){//头像
				var act="upheaderimg";
				var cutWidth=300;
				var cutHeight=300;
			}
			if($scope.tid=="tutorhead"){//个人中心背景图
				var act="uptutorheaderimg";
				var cutWidth=720;
				var cutHeight=510;
			}
			if($scope.tid=="tutorindex"){//导师主页背景图
				var act="uptutorindeximg";
				var cutWidth=720;
				var cutHeight=640;
			}
			$cordovaCamera.getPicture({
				quality: 100,
				destinationType: Camera.DestinationType.FILE_URI,
				targetWidth: 1024,
				targetHeight: 1024,
				allowEdit: false,
				sourceType: 1,
				Direction: 1
			}).then(function (res) {
				console.log(res);
				plugins.crop(function (imageURI) {
					if(imageURI==null||imageURI==""){
						return;
					}
					var url = $rootScope.site + "index.php?app=usermyapp&act=upfile&callback=JSON_CALLBACK";
					$http.jsonp(url).success(function (data) {
						console.log(data);
						if (data.done) {
							$rootScope.ECM_ID = data.retval.userid;
							$rootScope.HTTP_USER_AGENT = data.retval.HTTP_USER_AGENT;
							var uri = $rootScope.site + 'index.php?app=appupload&ajax=ajax&XDEBUG_SESSION_START=ECLIPSE_DBGP';
							console.log(uri);
							var options = new FileUploadOptions();
							options.fileKey = "file";
							options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
							options.mimeType = "text/plain";
							var params = {};
							params.ECM_ID = $rootScope.ECM_ID;
							params.HTTP_USER_AGENT = $rootScope.HTTP_USER_AGENT;
							params.belong = 5;
							params.item_id = $scope.user.user_id;
							options.params = params;
							ProgressIndicator.showProgressBar('正在上传...');
							$cordovaFileTransfer.upload(uri, imageURI, options).then(function (r) {
								console.log("Code = " + r.responseCode);
								console.log("Response = " + r.response);
								console.log("Sent = " + r.bytesSent);
								ProgressIndicator.hide();
								data = JSON.parse(r.response);
								if (data.done) {
									if($scope.tid=="head"){//头像
										$scope.user.headimgurl = data.retval.file_path;
									}
									if($scope.tid=="tutorhead"){//个人中心背景图
										$scope.user.tutor_head = data.retval.file_path;
									}
									if($scope.tid=="tutorindex"){//导师主页背景图
										$scope.user.tutor_index = data.retval.file_path;
									}
									console.log("sender.src = " + $scope.user.headimgurl);
									url = $rootScope.site + "index.php?app=usermyapp&act="+act+"&headerimg=" + data.retval.file_path + "&callback=JSON_CALLBACK";
									$http.jsonp(url).success(function (data) {
										showtoast('上传成功');
										$scope.show = false;
									});
								}
								else {
									showtoast(data.msg);
								}
							}, function (error) {
								console.log("An error has occurred: Code = " + error.code);
								console.log("upload error source " + error.source);
								console.log("upload error target " + error.target);
								ProgressIndicator.hide();
								showtoast('图片选择失败，' + error);
								$scope.show = false;
							}, function (progressEvent) {
								ProgressIndicator.setProgress(progressEvent.loaded / progressEvent.total);
							});
						}
					});
				}, function (e) {
				}, res, {width: cutWidth, height: cutHeight, quality: 80});


			});

		}

		//本地图片
		$scope.localpic = function (e) {
			if (e) {
				e.stopPropagation();
			}
			var act="upheaderimg";
			var cutWidth=300;
			var cutHeight=300;
			if($scope.tid=="head"){//头像
				var act="upheaderimg";
				var cutWidth=300;
				var cutHeight=300;
			}else if($scope.tid=="tutorhead"){//个人中心背景图
				var act="uptutorheaderimg";
				var cutWidth=720;
				var cutHeight=510;
			}else if($scope.tid=="tutorindex"){//导师主页背景图
				var act="uptutorindeximg";
				var cutWidth=720;
				var cutHeight=640;
			}
			window.imagePicker.getPictures(
					function (results) {
						plugins.crop(function (newpath) {
							if(newpath==null||newpath==""){
								return;
							}
							var url = $rootScope.site + "index.php?app=usermyapp&act=upfile&callback=JSON_CALLBACK";
							$http.jsonp(url).success(function (data) {
								console.log(data);
								if (data.done) {
									var imageURI = newpath;
									$rootScope.ECM_ID = data.retval.userid;
									$rootScope.HTTP_USER_AGENT = data.retval.HTTP_USER_AGENT;
									console.log(imageURI);
									var uri = $rootScope.site + 'index.php?app=appupload&ajax=ajax&XDEBUG_SESSION_START=ECLIPSE_DBGP';

									var options = new FileUploadOptions();
									options.fileKey = "file";
									options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
									options.mimeType = "text/plain";

									var params = {};
									params.ECM_ID = $rootScope.ECM_ID;
									params.HTTP_USER_AGENT = $rootScope.HTTP_USER_AGENT;
									params.belong = 5;
									params.item_id = $scope.user.user_id;
									options.params = params;

									ProgressIndicator.showProgressBar('正在上传...');
									$cordovaFileTransfer.upload(uri, imageURI, options).then(function (r) {
										console.log("Code = " + r.responseCode);
										console.log("Response = " + r.response);
										console.log("Sent = " + r.bytesSent);
										ProgressIndicator.hide();
										data = JSON.parse(r.response);
										if (data.done) {
											if($scope.tid=="head"){//头像
												$scope.user.headimgurl = data.retval.file_path;
											}
											if($scope.tid=="tutorhead"){//个人中心背景图
												$scope.user.tutor_head = data.retval.file_path;
											}
											if($scope.tid=="tutorindex"){//导师主页背景图
												$scope.user.tutor_index = data.retval.file_path;
											}
											console.log("sender.src = " + $scope.user.headimgurl);
											url = $rootScope.site + "index.php?app=usermyapp&act="+act+"&headerimg=" + data.retval.file_path + "&callback=JSON_CALLBACK";
											$http.jsonp(url).success(function (data) {
												showtoast('上传成功');
												$scope.show = false;
											});
										}
										else {
											showtoast(data.msg);
										}
									}, function (error) {
										console.log("An error has occurred: Code = " + error.code);
										console.log("upload error source " + error.source);
										console.log("upload error target " + error.target);
										ProgressIndicator.hide();
										showtoast('图片选择失败，' + error);
										$scope.show = false;
									}, function (progressEvent) {
										ProgressIndicator.setProgress(progressEvent.loaded / progressEvent.total);
									});
								}
							});
						}, function (e) {
						}, results[0], {width: cutWidth, height: cutHeight, quality: 80});
					}, function (error) {
						console.log('Error: ' + error);
					}, {
						maximumImagesCount: 1,
						width: 1024,
						height: 1024
					}
			);
		}


	}
	catch (e) {
		showcatch(e);
	}
});
BaseUedit.BaseUeditDefault = function ($q,$http,$rootScope) {
	var def = $q.defer();
	var data = [];

	var url = SITE_URL + "index.php?app=baseloginapp&act=tutorbase&callback=JSON_CALLBACK";
	$http.jsonp(url).success(function (data) {
		if (data.done&&data.retval==true) {
			$rootScope.istutor=true;
		}
		def.resolve('');
	}).error(function(){
		def.resolve('');
	});

	return def.promise;
}
BaseUedit.BaseUeditData = function ($q, $http, $stateParams) {
	try {
		var data = [];
		var def = $q.defer();
		if (window.localStorage.u == undefined || window.localStorage.u == null || window.localStorage.u == "") {
			$rootScope.go("loginmain");
		} else {
			def.resolve(data);
		}

		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}